package com.edu.cold.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.cold.common.user.dao.SysUserTokenDao;
import com.edu.cold.common.user.entity.SysUserTokenEntity;
import com.edu.cold.common.utils.Result;
import com.edu.cold.common.utils.TokenGenerator;
import com.edu.cold.user.service.SysUserTokenService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 用户Token Service层接口 实现类
 * </p>
 *
 * @author jpge
 * @since 2024-04-05
 */
@Service("sysUserTokenService")
public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService {
    //12小时后过期
    private final static int EXPIRE = 3600 * 12;

    /**
     * 生成token
     *
     * @param userId 用户ID
     */
    @Override
    public Result createToken(long userId) {
        //生成一个token
        String token = TokenGenerator.generateValue();

        //当前时间
        Date now = new Date();
        //过期时间
        Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

        //判断是否生成过token
        SysUserTokenEntity tokenEntity = this.getById(userId);
        if (tokenEntity == null) {
            tokenEntity = new SysUserTokenEntity();
            tokenEntity.setUserId(userId);
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);

            //保存token
            this.save(tokenEntity);
        } else {
            tokenEntity.setToken(token);
            tokenEntity.setUpdateTime(now);
            tokenEntity.setExpireTime(expireTime);

            //更新token
            this.updateById(tokenEntity);
        }

        return Result.ok().put("token", token).put("expire", EXPIRE);
    }

    /**
     * 退出，删除token记录
     */
    @Override
    public void logout(String token) {
        this.getBaseMapper().deleteByToken(token);
    }
}
